# Solver.py: class with the maze solver user interface
# Written by Victor Luna Freire - victorclf@live.com
# Copyright (c) Victor Luna Freire. All rights reserved.
# Wednesday, November 04, 2009 6:29:09 PM
# Python 2.6.2

import pygame

import Defines
import Input
import Maze
import MazePlayerAI
import SpecialText
import Shared
import Gamescript


class Solver:
    def __init__(self):
        self.sfont_big = pygame.font.Font('MAZE.TTF', 36)
        self.text_solving = SpecialText.GlowingText(self.sfont_big, "Solucionando", (219, 80), 2.8, (130, 0, 0), (255, 0, 0))
        self.text_backtracking = SpecialText.GlowingText(self.sfont_big, "Backtracking", (219, 435), 3.4, (0, 90, 0), (0, 255, 0))
            
    
    def new_solver(self):
        center = Shared.gridcenter((self.player.x, self.player.y))
        self.player.x = center[0]
        self.player.y = center[1]
        Maze.Maze.start_solving(self.player)
        self.state.append(self.solver)
    
    
    def solver_input(self, mult_factor):
        keep_running = Input.Input.get_input()
                
        if Input.Input.check_once(pygame.K_ESCAPE):
            keep_running = False
            
        if Input.Input.check_once(pygame.K_SPACE):
            current_speed = Maze.Maze.player_ai.adjust_speed(Defines.MAZE_PLAYER_AI_TURN_SPEED_INCREMENT, Defines.MAZE_PLAYER_AI_MOVE_SPEED_INCREMENT)
            if (current_speed[0] > Defines.MAZE_PLAYER_AI_TURN_SPEED_INTERVAL[1]
             or current_speed[1] > Defines.MAZE_PLAYER_AI_MOVE_SPEED_INTERVAL[1]):
                Maze.Maze.solver_all_steps()
            
                            
        #if Input.Input.check(pygame.K_p):
        #    self.new_game()
        
        return keep_running
    
    
    def solver(self):
        if not self.solver_input(self.mult_factor): return False
        
        self.screen.fill((0, 0, 0))
        
        self.process_world()
        if not Maze.Maze.solver_next_step(self.mult_factor):
            Gamescript.SoundDirector.sound_event('exitfound')
            return False
        self.text_solving.main(self.screen, self.mult_factor)
        if Maze.Maze.backtracking: self.text_backtracking.main(self.screen, self.mult_factor)
        
        pygame.display.flip()
        return True

